package com.lyy.king.auth.config;

import com.lyy.king.auth.utils.RsaUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @BelongsProject: springcloud-learning
 * @BelongsPackage: com.cdx.auth.config
 * @Author: cdx
 * @CreateTime: 2022-06-05  15:07
 * @Description:
 * @Version: 1.0
 */
@ConfigurationProperties(prefix = "learning.jwt")
public class JwtProperties implements InitializingBean {
    /**
     * 私钥路径
     */
    private String privateKeyPath;
    /**
     * 公钥路径
     */
    private String publicKeyPath;
    /**
     * 过期时间
     */
    private Integer expire;

    private PublicKey publicKey;
    private PrivateKey privateKey;


    public Integer getExpire() {
        return expire;
    }

    public void setExpire(Integer expire) {
        this.expire = expire;
    }

    public PublicKey getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(PublicKey publicKey) {
        this.publicKey = publicKey;
    }

    public PrivateKey getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(PrivateKey privateKey) {
        this.privateKey = privateKey;
    }

    public String getPrivateKeyPath() {
        return privateKeyPath;
    }

    public void setPrivateKeyPath(String privateKeyPath) {
        this.privateKeyPath = privateKeyPath;
    }

    public String getPublicKeyPath() {
        return publicKeyPath;
    }

    public void setPublicKeyPath(String publicKeyPath) {
        this.publicKeyPath = publicKeyPath;
    }


    /**
     * 读取文件，初始化公钥和秘钥
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            setPublicKey(RsaUtils.getPublicKey(publicKeyPath));
            setPrivateKey(RsaUtils.getPrivateKey(privateKeyPath));
        }catch (Exception e){
            throw new RuntimeException("init jwt key error",e);
        }
    }
}
